//
// Created by xusun on 2022/1/29.
//

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
struct ListNode {
    int val;
    ListNode *next;

    ListNode() : val(0), next(nullptr) {}

    ListNode(int x) : val(x), next(nullptr) {}

    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution206 {
public:
    ListNode *reverseList(ListNode *head) {
        if (!head)return head;
        if (!head->next)return head;
        ListNode *head_tmp = new ListNode();
        head_tmp->next = head;
        ListNode *tmp = head;
        ListNode *tmp_next = head->next;
        head_tmp->next = nullptr;
        while (tmp) {
            tmp->next = head_tmp->next;
            head_tmp->next = tmp;
            tmp = tmp_next;
            if (tmp_next)
                tmp_next = tmp_next->next;
        }
        return head_tmp->next;
    }
};